package com.jichangxiu.server.platform.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jichangxiu.common.entity.bo.PageData;
import com.jichangxiu.common.entity.bo.Record;
import com.jichangxiu.common.utils.StrUtils;
import com.jichangxiu.framework.mybatis.BaseServiceImpl;
import com.jichangxiu.server.platform.entity.dto.PlatformLogQueryDto;
import com.jichangxiu.server.platform.entity.map.PlatformLogBasic;
import com.jichangxiu.server.platform.entity.po.PlatformLog;
import com.jichangxiu.server.platform.entity.vo.PlatformLogQueryVo;
import com.jichangxiu.server.platform.mapper.PlatformLogMapper;
import com.jichangxiu.server.platform.service.PlatformLogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * PlatformLogServiceImpl:
 *
 * @author Jcx
 * @create 2023-02-26 20:57
 */
@Service
public class PlatformLogServiceImpl extends BaseServiceImpl<PlatformLogMapper, PlatformLog> implements PlatformLogService {

    @Resource
    private StrUtils strUtils;

    @Resource
    private PlatformLogMapper platformLogMapper;

    /**
     * 保存日志
     *
     * @param record 日志信息载体
     * @return 是否成功
     */
    @Override
    public Boolean saveLog(Record record) {
        PlatformLog platformLog = record.getModel(PlatformLog.class);
        if (ObjectUtil.isEmpty(platformLog.getResult())) {
            platformLog.setResult("0");
        }
        if (ObjectUtil.isEmpty(platformLog.getException())) {
            platformLog.setException("0");
        }
        platformLog.setLogNo(strUtils.createNo("log"));
        return save(platformLog);
    }

    /**
     * 分页日志列表
     *
     * @param platformLogQueryVo 日志对象
     * @return 分页日志列表
     */
    @Override
    public PageData<PlatformLogQueryDto> pageList(PlatformLogQueryVo platformLogQueryVo) {
        Page<PlatformLog> page = startPage();
        List<PlatformLog> platformLogList = platformLogMapper.pageList(PlatformLogBasic.INSTANCE.toPlatformLog(platformLogQueryVo), page);
        List<PlatformLogQueryDto> platformLogQueryDtoList = PlatformLogBasic.INSTANCE.toPlatformLogQueryDtoList(platformLogList);
        return PageData.build(new Page<>(page.getCurrent(), page.getSize(), page.getTotal()), platformLogQueryDtoList);
    }

    /**
     * 根据ID获取日志详情
     *
     * @param logId 日志ID
     * @return 日志详情
     */
    @Override
    public PlatformLogQueryDto info(String logId) {
        PlatformLog platformLog = platformLogMapper.info(logId);
        return PlatformLogBasic.INSTANCE.toPlatformLogQueryDto(platformLog);
    }

}
